package com.devotion.velocity.framework;

import java.util.Map;

import com.devotion.velocity.annotation.Action;
import com.devotion.velocity.util.ClassFilter;
import com.devotion.velocity.util.ClassScanner;
import com.devotion.velocity.util.InstanceUtil;

public class WebApplicationContext extends ApplicationContext implements WebContext {
    private static Map<String, Class> actionClassMap;
    private static String[] defaultActionPackages = {"com.devotion.velocity.action"};

    public void init() {
        init(defaultActionPackages);
    }

    public void init(String[] actionPackages) {
        init(ApplicationContext.defaultPackages, actionPackages);
    }

    public void init(String[] beanPackages, String[] actionPackages) {
        init(beanPackages, actionPackages, ApplicationContext.filter);
    }

    public void init(String[] beanPackages, String[] actionPackages, ClassFilter filter) {
        super.init(beanPackages, filter);
        actionClassMap = ClassScanner.packageScan(actionPackages, filter, Action.class);
    }

    public Object getActionInstance(String actionName) {
        Object action = null;
        try {
            Class actionClass = actionClassMap.get(actionName);
            if (actionClass == null) {
                return null;
            }
            action = actionClass.newInstance();
            InstanceUtil.referenceInject(action, ApplicationContext.instanceMap);
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return action;
    }

}
